<?php
class Customers extends MY_Controller{

	function __construct(){
		parent::__construct();

		if(!$this->checkUserStatus()){
			redirect('login');
		}else{
			$this->aauth->update_activity();
		}

	}

	function index(){
		$data['toDoList'] = $this->get_tasks();
		$data['customers'] = customer::find('all');
		$data['title'] = $this->title('Clients');
		$data['user'] = $this->aauth->get_user();
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/customers/index');
		$this->load->view($this->theme.'/footer');


	}



	function modification(){
		if(!$_POST){
			redirect("error_404");
		}else{

			$pk = $_POST['pk'];
			$name = $_POST['name'];
			$value = $_POST['value'];
			if(!empty($value) || $name =='comment' || $name == 'telephone' || $name == 'address' || $name == 'email') {
				if($name == 'email'){
					if($this->checkValidEamil($value)){
						$item = customer::find_by_id($pk);
						$item->$name = $value;
						$item->save();
						//header('HTTP 200 OK', true, 200);
						echo json_encode(array('name'=>$name,'value'=>$value,'id'=>$pk));

					}else{
						//header('HTTP 400 Bad Request', true, 400);
						echo "Le format d'email n'est pas correct!";
					}
				}else{
					$item = customer::find_by_id($pk);
					$item->$name = $value;
					$item->save();
					//header('HTTP 200 OK', true, 200);
					echo json_encode(array('name'=>$name,'value'=>$value,'id'=>$pk));
				}
			} else {

				//header('HTTP 400 Bad Request', true, 400);
				echo "Ce champ est obligatoire!";
			}

		}
	}

	function selectData($item = null){
		if($item == null)
		redirect('error_404');

		$data = $this->get_selectData($item);

		if($data){
			echo json_encode($data);
		}else{
			header('HTTP 400 Bad Request', true, 400);
			echo "Interne Errors !";
		}

	}

	private function get_selectData($item)
	{
		$d = array();

		switch ($item):

		case 'gender':
			$data = gender::find('all');
			foreach ($data as $i){
				$d[] = array('value'=>$i->id,'text'=>$i->gender);
			}
			break;
		case 'statut':
			$data = statut::find('all');
			foreach ($data as $i){
				$d[] = array('value'=>$i->id,'text'=>$i->statut);
			}
			break;

		case 'contract':
			$data = contract::find('all');
			foreach ($data as $i){
				$d[] = array('value'=>$i->id,'text'=>$i->contract);
			}
			break;

		case 'lang':
			$data = lang::find('all');
			foreach ($data as $i){
				$d[] = array('value'=>$i->id,'text'=>$i->lang);
			}
			
			endswitch;

			return $d;
	}








	function addCustomer(){
		$this->load->library('form_validation');
		$this->form_validation->set_rules('firstname','nom ', 'required');
		$this->form_validation->set_rules('lastname','prénom', 'required');
		$this->form_validation->set_rules('email','email', 'valid_email');
		$this->form_validation->set_error_delimiters('<p class="text-red">', '</p>');
		$this->form_validation->set_message('rule', 'Error Message');
		if($_POST){
				
			if($this->form_validation->run() == false){

			}else{
				$lastname = $_POST['lastname'];
				$firstname = $_POST['firstname'];
				$telephone = $_POST['telephone'];
				$email = $_POST['email'];
				$gender_id = $_POST['gender'];
				$address = $_POST['address'];
				$comment = $_POST['comment'];
				$company = $_POST['company'];
				$data = array('id'=>'','lastname'=>$lastname,'firstname'=>$firstname,'telephone'=>$telephone,'email'=>$email,'address'=>$address,'gender_id'=>$gender_id,'statut_id'=>1,'comment'=>$comment,'company'=>$company,'lang_id'=>1);
				$newCustomer = customer::create($data);
				if($newCustomer){
					redirect('customers');
				}else{
					$data['errors'] = array('il y a une erreur');
				}
			}
		}

		$data['toDoList'] = $this->get_tasks();
		$data['customers'] = customer::find('all');
		$data['title'] = $this->title('Clients');
		$data['user'] = $this->aauth->get_user();
		$data['gender'] = gender::find('all');
		$data['contract'] = contract::find('all');
		$data['agent'] = agent::find('all');
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/customers/addCustomer');
		$this->load->view($this->theme.'/footer');

	}

	function addProperty ()
	{
		if(!$client_id){
			redirect('customers');
		}else{
			if($_POST){
					
			}
			$data['toDoList'] = $this->get_tasks();
			$data['customers'] = customer::find('all');
			$data['title'] = $this->title('Clients');
			$data['user'] = $this->aauth->get_user();
			$data['category'] = category::find('all');
			$this->load->view($this->theme.'/header',$data);
			$this->load->view($this->theme.'/customers/addProperty');
			$this->load->view($this->theme.'/footer');
		}
	}

	function deleteCustomer(){
		if(!$_POST){
			redirect('error_404');
		}else{
			$id = $_POST['id'];
			$c = customer::find_by_id($id);
			$c->delete();
			$properties = property::find('all',array('conditions'=>array('customer_id'=>$id)));
			if($properties){
				foreach ($properties as $p){
				$item = property::find($p->id);
				$item ->customer_id = null;
				$item->save();
				}
			}
			echo json_encode(true) ;
		}
	}

	private function checkValidEamil($email)
	{
		return filter_var($email, FILTER_VALIDATE_EMAIL);

	}
}